# SPDX-License-Identifier: MIT
"""Data models for PMake manifest and build graph."""

from __future__ import annotations

from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, List, Optional


@dataclass(slots=True)
class ProjectSection:
    """Top-level project metadata."""

    name: str
    version: Optional[str]
    store_dir: Path
    globals: List[Path] = field(default_factory=list)


@dataclass(slots=True)
class Target:
    """Represents a unit of work within the build graph."""

    name: str
    kind: str  # "essential" or "optional"
    deps: List[str] = field(default_factory=list)
    executor: str = "agent"
    output: Path = Path()
    success: Optional[Path] = None
    metadata: Dict[str, str] = field(default_factory=dict)

    def is_optional(self) -> bool:
        return self.kind.lower() == "optional"

    def is_essential(self) -> bool:
        return self.kind.lower() == "essential"


@dataclass(slots=True)
class Manifest:
    """Fully parsed manifest."""

    project: ProjectSection
    targets: Dict[str, Target]
    extern: Dict[str, str]
    proposal: str
    roadmap: str
    manifest_path: Path

    def essential_targets(self) -> List[Target]:
        return [t for t in self.targets.values() if t.is_essential()]

    def optional_targets(self) -> List[Target]:
        return [t for t in self.targets.values() if t.is_optional()]
